package com.java.star.mall.front.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.java.star.mall.common.constant.web.PageConstant;
import com.java.star.mall.common.response.JsonPage;
import com.java.star.mall.common.response.JsonResult;
import com.java.star.mall.front.pojo.param.SearchParam;
import com.java.star.mall.front.pojo.vo.ProductDetailVO;
import com.java.star.mall.front.pojo.vo.ProductHotVO;
import com.java.star.mall.front.pojo.vo.ProductSwiperVO;
import com.java.star.mall.front.pojo.vo.ProductVO;
import com.java.star.mall.front.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品Controller
 */
@Api(value = "product", tags = "商品")
@RestController
@RequestMapping("/product")
@Validated
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "查询轮播商品列表")
    @ApiOperationSupport(order = 100)
    @GetMapping("/listSwiperProduct")
    public JsonResult<List<ProductSwiperVO>> listSwiperProduct() {
        List<ProductSwiperVO> productSwiperVOList = productService.findSwiperProduct();
        return JsonResult.ok(productSwiperVOList);
    }

    @ApiOperation(value = "分页查询热门推荐商品")
    @ApiOperationSupport(order = 200)
    @GetMapping("/listHotProduct")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    public JsonResult<JsonPage<ProductHotVO>> listHotProduct(
            @RequestParam(required = false, defaultValue = PageConstant.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<ProductHotVO> jsonPage = productService.listHotProduct(page, pageSize);
        return JsonResult.ok(jsonPage);
    }

    @ApiOperation(value = "查询商品详情")
    @ApiOperationSupport(order = 300)
    @GetMapping("/getProductDetail/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "productId", value = "商品ID", dataType = "int"),
    })
    public JsonResult<ProductDetailVO> getProductDetail(@PathVariable("productId") Integer productId) {
        ProductDetailVO productDetailVO = productService.getProductDetail(productId);
        return JsonResult.ok(productDetailVO);
    }

    @ApiOperation(value = "搜索商品")
    @ApiOperationSupport(order = 400)
    @GetMapping("/searchProduct")
    public JsonResult<JsonPage<ProductVO>> searchProduct(@Valid SearchParam searchParam) {
        JsonPage<ProductVO> jsonPage = productService.searchProduct(searchParam);
        return JsonResult.ok(jsonPage);
    }
}
